import re
import sys
import difflib
import time
import datetime
from numpy  import *
from pylab import *
sys.path.append("./pywikipedia")
import wikipedia as pywikibot

npoints = 2000
site = pywikibot.getSite()

lines = open("mostedited.csv").read().splitlines() #load the names of the most-edited articles
npages  = min(10, len(lines)) #Change the 50 to make it use more articles or run faster

iri = zeros((npoints,npages)) #inter-revision-interval (iri)
binnediri = zeros((npoints,npages)) 

for i in range(npages):
    p = pywikibot.Page(pywikibot.getSite(), lines[i]) 
    h = p.getVersionHistory(forceReload=True, getAll=True, reverseOrder=True)

    tsl = []
    for (revisionId, editTimestamp, username, editSummary, 
         editSize, editTags, content) in h:
        #Convert the Wiki ISO timestamp into Epoch time
        tsl.append(datetime.datetime.fromtimestamp(time.mktime(time.strptime(editTimestamp, "%Y-%m-%dT%H:%M:%SZ"))))

    bc = 0
    bins = array(0)
    cs = 0
    for point in range(min(len(tsl)-1,npoints)):
        diff = (tsl[point+1]-tsl[point])
        iri[point,i] = diff.seconds / 60.0
        if cs > 60*24: #This bins the data by day (60*24=number of minutes in a day)
            bc = bc + 1
            cs = 0
        else:
            cs = cs + iri[point,i]
        #Time series are binned such that revision dynamics can be observed
        binnediri[bc,i] = binnediri[bc,i] + 1

    print i

savetxt("binnediri.txt", binnediri)
savetxt("iri.txt", iri)

#Requires MATPLOTLIB to use. Plots the time-histogram of the revision history
plt.plot(binnediri[1:350,:])
plt.show()
plt.xlabel("day")
plt.ylabel("# of revisions")


